<?php

declare(strict_types=1);

namespace app\service;

use app\common\library\Wechat;
use app\service\DesEncrypt;
use GuzzleHttp\Client;
//凤奚api接口
class FXApiService extends \think\Service
{
    protected $url;
    protected $key;
    protected $secret;
    protected $dec;
    public static function getConfig($name)
    {
        $config = config('apikey.FX_API');
        return isset($config[$name]) ? $config[$name] : '';
    }
    /**
     * 注册
     *
     * @param [type] $mobile 电话号码
     * @param [type] $name  名称
     * @param [type] $password  密码
     * @param [type] $productTplCode   商品code
     * @return void
     */
    public static function regist($mobile, $name, $password, $productTplCode='411256')
    {
        $url = "/api/mechan/shiyue/regist";
        $data['mobile'] = $mobile;
        $data['name'] = $name;
        $data['password'] = $password;
        $data['productTplCode'] = $productTplCode;
        $res = self::post($url, $data);
        return $res;
    }
    /**
     * 获取产品佣金
     *
     * @param [type] $productIds
     * @return void
     */
    public static function queryProductCommissons($productIds)
    {
        $url = "/api/mechan/queryProductCommissons";
        $data['productIds'] = [$productIds];
        $res = self::post($url, $data,1);
        return $res;
    }
    /**
     * 获取渠道id对应的电话号码
     *
     * @param [type] $id
     * @return void
     */
    public static function getMemberInfo($id)
    {
        $url = "/api/mechan/getMemberInfo";
        $data['id'] = $id;
        // $data['mobile']=15973817745;
        $data['pageNum']=1;
        $data['pageSize']=10;
        $res = self::post($url, $data,1);
        return $res;
    }
    /**
     * 登录h5页面
     *
     * @param [type] $account
     * @return void
     */
    public static function getH5Login($account){
        
        $url = "/api/mechan/h5/login";
        $data['account'] = $account;
        $data['password']='Mm123456';
        $res = self::post($url, $data,1);
        return $res;
    }
    public static function post($url, $post_data_arr,$type=0)
    {
        $urls = self::getConfig('url') . $url;
        $post_data_str = json_encode($post_data_arr);
        if ($type==0) {
            $des = new DesEncrypt(self::getConfig('dec'));
            $post_data_str = $des->encrypt($post_data_str);
        }
        $client = new Client(['connect_timeout' => 10, 'timeout' => 10, 'verify' => false]);
        // var_dump($post_data_str);
        $head=[
            'accessKey'=>self::getConfig('key'),
            'accessSecret'=>self::getConfig('secret'),
        ];
        if($type==1){
            $head['Content-Type']='application/json';
        }
   
        try {
            $response = $client->request('POST', $urls,   [
                "body" =>$post_data_str,
                'headers'  => $head
            ]);
           
            $response_data = $response->getBody()->getContents();
            $response_data = json_decode($response_data, true);
            // var_dump( $response_data);
            if ($response_data["httpCode"] == "200") {
                return ["status" => "1", "msg" => "请求成功", "data" => $response_data['data']];
            } else {
                return ["status" => "0", "msg" => "请求错误:" . $response_data["msg"], "data" => ""];
            }
        } catch (\Exception $e) {
            $response_data = $e->getMessage();
            return ["status" => "0", "msg" => "请求错误:" . $response_data, "data" => ""];
        }
    }
}
